package il.ac.mta.javaweb.ex1.logic.players;

import il.ac.mta.javaweb.ex1.logic.grids.BattleShipsGrid;
import il.ac.mta.javaweb.ex1.logic.grids.MarkingGrid;

import java.util.ArrayList;
import java.util.List;

public abstract class PlayerBase
{
	private String name;
	private BattleShipsGrid ownGrid;
	private List<MarkingGrid> enemiesMarkingGrids = new ArrayList<MarkingGrid>();

	public void setName(String name)
	{
		this.name = name;
	}

	public String getName()
	{
		return name.substring(0, 1).toUpperCase() + name.substring(1);
	}

	public void setOwnGrid(BattleShipsGrid ownGrid)
	{
		this.ownGrid = ownGrid;
	}

	public BattleShipsGrid getOwnGrid()
	{
		return ownGrid;
	}

	public void addEnemyGrid(MarkingGrid enemyGrid)
	{
		this.enemiesMarkingGrids.add(enemyGrid);
	}

	public List<MarkingGrid> getEnemiesMarkingGrids()
	{
		List<MarkingGrid> tempEnemiesMarkingGrids = new ArrayList<MarkingGrid>(enemiesMarkingGrids);
		for (MarkingGrid grid : tempEnemiesMarkingGrids)
		{
			if (grid.getPlayer().getOwnGrid().allShipsSunk())
			{
				this.enemiesMarkingGrids.remove(grid);
			}
		}

		tempEnemiesMarkingGrids = null;

		return this.enemiesMarkingGrids;
	}

	@Override
	public String toString()
	{
		return getName();
	}
}
